package com.tous.common.core.exception;

import lombok.Getter;
import org.springframework.http.HttpStatus;

import java.io.Serial;

/**
 * 自定义异常
 *
 * 异常code 规则：
 * 1. 异常code 必须是全局唯一的
 * 2. 异常code 必须是字符串类型
 * 3. 异常code 必须是有意义的，能够描述异常的原因
 * 4. 异常code 必须是固定长度的，比如 5 位数字
 * 5. 异常code 必须是数字类型
 *
 * @author 73578
 */
@Getter
public class BaseWebException extends RuntimeException {

  @Serial
  private static final long serialVersionUID = -7972131521045668011L;

  private final HttpStatus httpStatus;

  // 自定义一个全局唯一的code，
  private String code;

  public BaseWebException() {
    httpStatus = HttpStatus.BAD_REQUEST;
  }

  public BaseWebException(HttpStatus httpStatus) {
    this.httpStatus = httpStatus;
  }

  public BaseWebException(String message, HttpStatus httpStatus) {
    super(message);
    this.httpStatus = httpStatus;
  }

  public BaseWebException(String message, Throwable cause, HttpStatus httpStatus) {
    super(message, cause);
    this.httpStatus = httpStatus;
  }

  public BaseWebException(Throwable cause, HttpStatus httpStatus) {
    super(cause);
    this.httpStatus = httpStatus;
  }

  public BaseWebException(String message, Throwable cause, boolean enableSuppression,
                          boolean writableStackTrace, HttpStatus httpStatus) {
    super(message, cause, enableSuppression, writableStackTrace);
    this.httpStatus = httpStatus;
  }

    public void setCode(String code) {
    this.code = code;
  }
}